package cpre556.project2.eclipsePlugin.support;

import java.util.Vector;

/**
 * 
 */
public class RejectVulnerability{
	/**
	 * Saver instance
	 */
	private Saver saver;
	
	/**
	 * Constructor
	 * @param s Saver instance
	 */
	public RejectVulnerability(Saver s){
		saver = s;
	}
	
	/**
	 * 
	 * @param rejectedVulnerbility
	 */
	public void rejectVulnerability(Vulnerability rejectedVulnerbility){
		Vector<Vulnerability> allVulnerbilities = new Vector<Vulnerability>();
		
		XMLtoVulnerabilitiesVector xmlReader = new XMLtoVulnerabilitiesVector(saver.getFile());
		allVulnerbilities = xmlReader.getVulnerabilities();
		
		int size = allVulnerbilities.size();
		int key = rejectedVulnerbility.getLineNumber();
						
		for(int i = 0; i < size; i++){
			if(key == (allVulnerbilities.get(i)).getLineNumber()){
				if(rejectedVulnerbility.getFunctionName().equalsIgnoreCase((allVulnerbilities.get(i)).getFunctionName())){
					allVulnerbilities.remove(i);
					size = size-1;
					
					XMLWriter xmlWriter = new XMLWriter(allVulnerbilities, saver);
					xmlWriter.writeXML();
					
					XMLIgnoreWriter xmlIgnoreWriter = new XMLIgnoreWriter(saver);
					xmlIgnoreWriter.writeXML(rejectedVulnerbility);					
				}
			}
		}
	}
	
	/**
	 * 
	 * @param restoreVulnerbility
	 */
	public void undoRejectedVulnerability(Vulnerability restoreVulnerbility){
		Vector<Vulnerability> allVulnerbilities = new Vector<Vulnerability>();
		Vector<IgnoredVulnerability> ignoredVulnerbilities = new Vector<IgnoredVulnerability>();
		
		XMLtoVulnerabilitiesVector xmlAllReader = new XMLtoVulnerabilitiesVector(saver.getFile());
		allVulnerbilities = xmlAllReader.getVulnerabilities();	
		
		XMLIgnoreReader xmlReader = new XMLIgnoreReader(saver);
		ignoredVulnerbilities = xmlReader.getIgnoredVulnerabilitiesVector();
			
		@SuppressWarnings("unused")
		int size = ignoredVulnerbilities.size();
		
		int key = restoreVulnerbility.getLineNumber();
		
		boolean notDone = true;
		int count = 0;
		
		while(notDone){
			if(key == ((Vulnerability)ignoredVulnerbilities.get(count)).getLineNumber()){
				if(restoreVulnerbility.getFunctionName().equalsIgnoreCase(((Vulnerability)ignoredVulnerbilities.get(count)).getFunctionName())){
					allVulnerbilities.add(ignoredVulnerbilities.get(count));
					
					ignoredVulnerbilities.remove(count);
					notDone = false;					
					
					VulnerabilitySorter sorter = new VulnerabilitySorter();
					allVulnerbilities = sorter.sortByLineNumber(allVulnerbilities);
					
					XMLWriter xmlWriter = new XMLWriter(allVulnerbilities, saver);
					xmlWriter.writeXML();
					
					int newSize = ignoredVulnerbilities.size();
					
					if(newSize > 0){
						
						XMLIgnoreWriter xmlIgnoreWriter = new XMLIgnoreWriter(saver);
						xmlIgnoreWriter.writeBlankXML();
						
						for(int j=0; j<newSize; j++){
							Vulnerability nextIgnoredVulnerbility = (Vulnerability)ignoredVulnerbilities.get(j);
													
							xmlIgnoreWriter.writeXML(nextIgnoredVulnerbility);				
						}
					}
					else{
						XMLIgnoreWriter xmlIgnoreWriter = new XMLIgnoreWriter(saver);
						xmlIgnoreWriter.writeBlankXML();
					}						
				}
			}
			count++;
		}	
	}
}
